package com.corner.presenter.imp;

import android.net.Uri;

import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.cornerservice.helper.InfoHelper;
import com.corner.model.cornerservice.helper.UserHelper;
import com.corner.presenter.IProfilePresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.view.IProfileView;
import com.corner.utils.DateFormatUtil;

import java.util.Date;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/13，16.40.
 * 描述：
 */
public class ProfilePresenter extends BasePresenter<IProfileView> implements IProfilePresenter {

    public static final int MAX_TIME_SPAN = 20000;
    private boolean hasMore = true;
    private List<Info> infos;
    private Long userId;
    private boolean isLoading;
    private double lastRefreshTime;

    @Override
    public void refresh(Long userId) {
        if (isLoading) {
            return;
        }
        if (DateFormatUtil.getCurrentTimeInMills() - lastRefreshTime < MAX_TIME_SPAN) {
            view.refreshComplete();
            return;
        }

        isLoading = true;
        lastRefreshTime = DateFormatUtil.getCurrentTimeInMills();
        //刷新infos
        this.userId = userId;
        InfoHelper.getInfos(this.userId, 10, 0).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Info>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null)
                    view.refreshComplete();
                isLoading = false;
            }

            @Override
            public void onNext(List<Info> infos) {
                if (view != null) {
                    if (infos != null) {
                        ProfilePresenter.this.infos = infos;
                        hasMore = infos.size() >= 10;
                        view.showRefreshInfo(infos);
                        view.showLoadEnable(hasMore);
                    }
                    view.refreshComplete();
                }
                isLoading = false;
            }
        });
        //刷新用户昵称及头像
        UserHelper.getUser(this.userId).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<User>() {
            @Override
            public void call(User user) {
                if (view != null) {
                    if (user.getAvaterUrl() != null)
                        view.setAvaterUri(Uri.parse(user.getAvaterUrl()));
                    view.setNickName(user.getUsername());
                    view.setCreateTime(new Date(user.getCreateAt()));
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
            }
        });
        Observable.zip(InfoHelper.getInfoCount(this.userId), InfoHelper.getPicCount(this.userId), new Func2<Long, Long, Integer[]>() {
            @Override
            public Integer[] call(Long integer, Long integer2) {
                Integer[] count = new Integer[2];
                count[0] = integer.intValue();
                count[1] = integer2.intValue();
                return count;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer[]>() {
            @Override
            public void call(Integer[] count) {
                if (view != null)
                    view.setInfosCount(count[0], count[1]);
            }
        });
        //刷新消息数量
    }

    @Override
    public void loadMore() {
        if (!hasMore || infos == null) {
            view.loadMoreComplete();
            return;
        }
        if (isLoading) {
            return;
        }
        isLoading = true;
        InfoHelper.getInfos(userId, 10, infos.size()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Info>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null)
                    view.loadMoreComplete();
                isLoading = false;
            }

            @Override
            public void onNext(List<Info> infos) {
                if (view != null) {
                    if (infos != null) {
                        if (infos.size() == 0) {
                            hasMore = false;
                        } else {
                            ProfilePresenter.this.infos.addAll(infos);
                            view.showLoadMoreInfo(ProfilePresenter.this.infos);
                        }
                    }
                    view.showLoadEnable(hasMore);
                    view.loadMoreComplete();
                }
                isLoading = false;
            }
        });
    }
}
